<?php
namespace App\JdApi;
use App\JdService\jdService;
use App\JdLib\demo;
use App\YlyService\ylyPrint;
/**
 * Created by PhpStorm.
 * User: laodage
 * Date: 19-1-15
 * Time: 下午2:13
 */
class jdConfOrder extends jdService{

    public function run(){

        $redis = $this->getRedis();
        //$db = $this->getDb();
        $order = $redis->rpop('jdprintorder');
        if(empty($order)){
            return true;
        };
        $data = json_decode($order,true);
        $jd_param =  json_decode($data['jd_param_json'],true);
        $token = $data['token'];
        $orderid = $jd_param['billId'];
        $demo = new demo();
        $req = $demo->Order($orderid);
        if($req['code'] != 0){
            file_put_contents('../../Log/jdreturn'.date('Y-m-d').'.log',date('Y-m-d H:i:s').'京东接口报错:'.$orderid.'--'.json_encode($req)."\n",FILE_APPEND);
            return true;
        }
        $orderData = $req['data'];
        $orderData = json_decode($orderData,true);
        $orderData = $orderData['result'];
        $orderDatas = str_replace("\\n",'',$orderData);
        $orderarr = json_decode($orderDatas,true);
        $shopid = $this->getShopid($token);
        if(empty($shopid)){
            file_put_contents('../../Log/shop_error'.date('Y-m-d').'.log',date('Y-m-d H:i:s').'用户'.$jd_param['user_nick'].'token为:'.$token.'未授权'."\n",FILE_APPEND);
            return true;
        }
        $printer = $this->getPrinter($shopid['shopid']);
        $shop = $this->getShopset($shopid['shopid']);

        if(empty($printer)){
            file_put_contents('../../Log/shop_error'.date('Y-m-d').'.log',date('Y-m-d H:i:s').'店铺打印机为空,订单id为:'.$orderid."\n",FILE_APPEND);
            return true;
        }

        foreach ($printer as $v){

            $content = ylyPrint::index($orderarr,$v,$jd_param['statusId'],$shop);

            $this->sentToPrint($v['machine_code'],$content,$v['access_token'],$orderid,$order);

        }



    }
}